package com.sduept.nwld.wave.util;

import java.util.regex.Pattern;


/**
 * @author     Author
 * @version    V1.0
 * @Date       2018年8月16日 下午2:58:04
 */
public class ProtectionNameParseUtil {
	
	
	/**
	 * 根据名字获取相应名字的枚举
	 * @param name
	 * @return
	 */
	public static EquipmentTypeEnum getEquipmentTypeEnum(String name){
		if (name.contains("短引线")) {
			if (name.matches(".*(主变|#)") && !name.contains("T区")) {
				return EquipmentTypeEnum.TYPE_POWER;
			} else {
				return EquipmentTypeEnum.TYPE_LINE;
			}
		}
		if (name.contains("断路器") || name.contains("开关")) {
			return EquipmentTypeEnum.TYPE_BREAKER;
		}
		if (name.contains("变") && !name.contains("油温") && !name.contains("温度")) {
			return EquipmentTypeEnum.TYPE_POWER;
		}
		if (name.contains("母")) {
			return EquipmentTypeEnum.TYPE_BUSBAR;
		}
		if (name.toUpperCase().contains("M")) {
			if(name.toUpperCase().contains("MM")){
				name=name.replace("MM", "");
			}
			if(name.endsWith("m")){
				name=name.substring(0, name.lastIndexOf("m"));
			}
			if(name.endsWith("M")){
				name=name.substring(0, name.lastIndexOf("M"));
			}
			String regex = ".*(m|M)(?!([0-9]|[a-zA-Z]|-|——)).*";
			if (name.matches(regex)) {
				return EquipmentTypeEnum.TYPE_BUSBAR;
			}
		}
		if (name.contains("抗") || name.contains("滤波器")) {
			return EquipmentTypeEnum.TYPE_COMPEN;
		}
		if (name.contains("线") || name.contains("回")) {
			return EquipmentTypeEnum.TYPE_LINE;
		}
		if (name.contains("发电机") || name.contains("电机") || name.contains("机组")) {
			return EquipmentTypeEnum.TYPE_HYDRO;
		}
		Pattern dlq = Pattern.compile("[0-9]{4}");
		if (dlq.matcher(name).find()) {
			return EquipmentTypeEnum.TYPE_BREAKER;
		}
		return EquipmentTypeEnum.TYPE_OTHER;
	}
	/**
	 * 根据名字获取设备类型
	 * @param name
	 * @return
	 */
	public static String getEquipmentType(String name){
		return getEquipmentTypeEnum(name).getDesc();
	}
	
	/**
	 * 设备类型枚举
	 * @author     Author
	 * @version    V1.0
	 * @Date       2018年8月16日 下午4:18:28
	 */
	public enum EquipmentTypeEnum{
		TYPE_OTHER(0, "未知"), 
		TYPE_BREAKER(1, "断路器"), 
		TYPE_POWER(2, "变压器"), 
		TYPE_BUSBAR(3, "母线"), 
		TYPE_LINE(4, "线路"), 
		TYPE_HYDRO(5, "发电机"), 
		TYPE_COMPEN(6, "电抗器");
		
		private int code;
		private String desc;

		private EquipmentTypeEnum(int code, String desc) {
			this.code = code;
			this.desc = desc;
		}

		public static EquipmentTypeEnum val(String desc) {
			for (EquipmentTypeEnum s : values()) {
				if (desc.equals(s.getDesc()))
					return s;
			}
			return null;
		}

		public static EquipmentTypeEnum valBycode(int code) {
			for (EquipmentTypeEnum s : values()) {
				if (code == s.getCode())
					return s;
			}
			return null;
		}

		public int getCode() {
			return code;
		}

		public void setCode(int code) {
			this.code = code;
		}

		public String getDesc() {
			return desc;
		}

		public void setDesc(String desc) {
			this.desc = desc;
		}

	}

}
